
package session.TX;

import entity.Places;
import entity.Reviews;
import entity.Stakeholder;
import entity.StakeholderTier;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateful
public class SurveySB implements
        SurveySBRemote {
    @PersistenceContext()
    EntityManager em;
    StakeholderTier stakeholderTier;
    Stakeholder stakeholder;
    Places place;
    

    public SurveySB() {}
    
    public Places getSpecificPlace(Long outletId) {

        Query q = em.createQuery("SELECT p FROM Places p");
        Places result = new Places();
        for(Object o: q.getResultList()){
            Places r = (Places)o;
        if (outletId.equals(r.getOutlet().getId())) {
            result = r;
            System.out.println("WTAAAAAAA" + r.getPlaceName());
            break;
        }
        }

        return result;
    }
    
    public int getTotalNoOfReviews(Places place) {
        int count = 0;
        for (Object o: place.getReviews()) {
            count++;
        }
        return count;
    }
    
    public Integer getNoOfReviews(Places place, int month, int year) {
        Calendar calendar = null;
        Integer count = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            count++;
        }
        }
        return count;
    }
    
    public List<Places> getAllPlaces() {
        Query q = em.createQuery("SELECT p FROM Places p");
        return q.getResultList();
    }
 
    public Double getAmbienceResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getAmbience();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
    
    public Double getOverallResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getOverall();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
    
    public Double getFoodResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
       for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getFood();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
    
     public Double getFoodResultAll(int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        List<Places> place = getAllPlaces();
        for (int x = 0; x < place.size(); x++){
            Places place1 = place.get(x);
           for (Object o: place1.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getFood();
            count++;
        }
        }
        }
        Double score = result/count;
        return score;
    }
    
    public Double getServiceResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
       for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getService();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
      
    public Double getServiceResultAll(int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        List<Places> place = getAllPlaces();
        for (int x = 0; x < place.size(); x++){
            Places place1 = place.get(x);
           for (Object o: place1.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getService();
            count++;
        }
        }
        }
        Double score = result/count;
        return score;
    }
     

        public Double getPricingResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getPricing();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
        
     public Double getPricingResultAll(int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        List<Places> place = getAllPlaces();
        for (int x = 0; x < place.size(); x++){
            Places place1 = place.get(x);
           for (Object o: place1.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getPricing();
            count++;
        }
        }
        }
        Double score = result/count;
        return score;
    }
     
     public Double getOverallAll(int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        List<Places> place = getAllPlaces();
        for (int x = 0; x < place.size(); x++){
            Places place1 = place.get(x);
           for (Object o: place1.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getOverall();
            count++;
        }
        }
        }
        Double score = result/count;
        return score;
    }
     
     public Double getAmbienceResultAll(int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        List<Places> place = getAllPlaces();
        for (int x = 0; x < place.size(); x++){
            Places place1 = place.get(x);
           for (Object o: place1.getReviews()) {
            Reviews r = (Reviews)o;
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getAmbience();
            count++;
        }
        }
        }
        Double score = result/count;
        return score;
    }


    @Remove
    public void remove() {
        System.out.println("SurveyBean:remove()");
    }

   

}